import useComputeForm from '@renderer/composables/useComputeForm'
import useStorage from '@renderer/composables/useStorage'
import { CacheEnum } from '@renderer/enum/CacheEnum'
import { ElMessage } from 'element-plus'

const { saveTableData } = useComputeForm()
const storage = useStorage()
const userInfo = await storage.get(CacheEnum.USER_INFO)
const companyInfo = await storage.get(CacheEnum.COMPANY_INFO)
export default () => {
  const downLoadFile = (fileBold: any, fileName: string) => {
    const blob = new Blob([fileBold], { type: `application/octet-stream;charset=utf-8` })
    const downloadElement = document.createElement('a')
    const href = window.URL.createObjectURL(blob)
    downloadElement.href = href
    downloadElement.download = fileName
    document.body.appendChild(downloadElement)
    downloadElement.click()
    document.body.removeChild(downloadElement)
    window.URL.revokeObjectURL(href)
  }
  const handleSaveTableData = (submitData: any[], submitType: number): Promise<void> => {
    return new Promise(async (resolve, reject) => {
      if (submitData.length) {
        await saveTableData({
          data: submitData,
          type: submitType,
        })
        resolve()
      } else {
        ElMessage({ type: 'error', message: '您未变更数据信息' })
        reject()
      }
    })
  }
  const checkRule = (ruleName: string) => {
    if (userInfo?.rule.includes(ruleName) || userInfo?.super_check == 1) {
      return true
    } else {
      return false
    }
  }
  const checkCompanyRule = (ruleFiled: string, successNumBer: number) => {
    if (companyInfo[ruleFiled] == successNumBer) {
      return true
    } else {
      return false
    }
  }
  const getFindNameType = (name: string) => {
    switch (name) {
      case '拆砌改':
        return 1
      case '灯具':
        return 2
      case '家电':
        return 3
      case '家具':
        return 4
      case '窗帘':
        return 5
      case '饰品':
        return 6
      case '五金':
        return 7
      case '石材':
        return 8
      case '集成顶':
        return 9
      case '合金门':
        return 10
      case '木门':
        return 11
      case '卫浴':
        return 12
      case '木地板':
        return 13
      case '瓷砖':
        return 14
      case '水电改造':
      case '地暖铺设':
        return 15
      case '防水立管':
        return 16
      case '贴砖辅料':
      case '铺贴工费':
        return 17
      case '木工材料':
        case '木工工费':
        return 18
      case '木工配套':
        return 19
      case '成品定制':
        return 20
      case '墙漆材料':
        return 21
      case '美缝':
        return 22
      case '杂项':
        return 23
      default:
        return 0
    }
  }
  return {
    downLoadFile,
    handleSaveTableData,
    getFindNameType,
    checkRule,
    checkCompanyRule,
  }
}
